package rs.fon.is.music.parser.threadpool;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import rs.fon.is.music.parser.MusicAlbumParser;

public class CustomThreadPoolExecutor extends ThreadPoolExecutor {

	public CustomThreadPoolExecutor(int corePoolSize, int maximumPoolSize,long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
		
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
		
		this.setRejectedExecutionHandler(new RejectedExecutionHandler() {

			@Override
			public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
				System.out.println("AlbumParser Rejected : " + ((MusicAlbumParser) r).getUrlAlbum());
				System.out.println("Waiting for a second !!");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("Lets add another time : " + ((MusicAlbumParser) r).getUrlAlbum());
				executor.execute(r);
			}

		});
	}

	@Override
	protected void beforeExecute(Thread t, Runnable r) {

		super.beforeExecute(t, r);

		// System.out.println("Perform beforeExecute() logic");

	}

	@Override
	protected void afterExecute(Runnable r, Throwable t) {

		super.afterExecute(r, t);

		if (t != null) {
			// System.out.println("Perform exception handler logic");
		}

		// System.out.println("Perform afterExecute() logic");

	}
}
